# desktop_app/services/crawlers/xiaohongshu.py
import requests
from bs4 import BeautifulSoup

def crawl(video_url):
    """小红书视频爬取方法"""
    try:
        print(f"正在爬取小红书视频: {video_url}")
        response = requests.get(video_url)
        html = response.text

        # 使用BeautifulSoup解析HTML
        soup = BeautifulSoup(html, 'html.parser')

        # 直接查找name属性为"og:type"的meta标签
        og_type_tag = soup.find('meta', attrs={'name': 'og:type'})

        # 获取content属性的值
        if og_type_tag:
            video_type = og_type_tag.get('content')
        else:
            raise ValueError("未找到og:type的meta标签")
        type = 'image'
        if video_type == 'video':
            type = 'video'
            video_tag = soup.find('meta', attrs={'name': 'og:video'})
            url = video_tag.get('content')
        else:
            # 查找所有name="og:image"的meta标签
            og_image_tags = soup.find_all('meta', attrs={'name': 'og:image'})
            # 提取所有content值
            # og_image_urls = [tag.get('content') for tag in og_image_tags]
            url = [tag.get('content') for tag in og_image_tags]
        return {
            'success': True,
            'platform': 'xiaohongshu',
            'video_id': 1,
            'type': type,
            'download_url': url,
            'title': "小红书笔记视频"
        }
    except Exception as e:
        return {
            'success': False,
            'error': str(e)
        }